#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Run a simple backup of the Bacula build directory but 
#   create three volumes and do six backups causing the
#   volumes to be recycled, and cycling through the volumes
#   twice. Tests maxvoljobs and volretention.
#
# Note, this test is a bit tricky, in that if you get too
#   much data in the regress/build directory, the test will
#   fail because it will run out of enough volumes to backup
#   all the data.
#
TestName="recycle-test"
JobName=Recycle
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/build" >${cwd}/tmp/file-list

change_jobname NightlySave $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=15 storage=File1
label storage=File1 volume=TestVolume001
label storage=File1 volume=TestVolume002
label storage=File1 volume=TestVolume003
label storage=File1 volume=TestVolume004
@# Note, this is going to fail if the pruning gets
@#  to tight because the saved volume is too large.
@#  In that case, either add another volume, or
@#  increase the volume capacity a bit.
update Volume=TestVolume001 volretention=5s
update Volume=TestVolume001 maxvolbytes=250000000
update Volume=TestVolume002 volretention=5s
update Volume=TestVolume002 maxvolbytes=250000000
update Volume=TestVolume003 volretention=5s
update Volume=TestVolume003 maxvolbytes=250000000
update Volume=TestVolume004 volretention=5s
update Volume=TestVolume004 maxvolbytes=250000000
sql
select mediaid,volumename,volstatus,volbytes,maxvolbytes,voljobs,volretention from Media;
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolBytes from Media;

@#setdebug level=110 storage=File1
setdebug level=60 dir
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
sql
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBytes from Media;

wait
messages
sql
select mediaid,volumename,volstatus,volbytes,maxvolbytes,voljobs,volretention from Media;
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBlocks,VolBytes from Media;

@sleep 12 
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
sql
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBlocks,VolBytes from Media;

wait
messages
sql
select mediaid,volumename,volstatus,volbytes,maxvolbytes,voljobs,volretention from Media;
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBlocks,VolBytes from Media;

@sleep 12
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
wait
messages
sql
select mediaid,volumename,volstatus,volbytes,maxvolbytes,voljobs,volretention from Media;
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBlocks,VolBytes from Media;

@sleep 12
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
wait
messages
sql
select mediaid,volumename,volstatus,volbytes,maxvolbytes,voljobs,volretention from Media;
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBlocks,VolBytes from Media;

@sleep 12
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
run job=$JobName storage=File1 level=full yes
wait
messages
sql
select mediaid,volumename,volstatus,volbytes,maxvolbytes,voljobs,volretention from Media;
select * from JobMedia;
select VolumeName,FirstWritten,LastWritten,VolStatus,VolBlocks,VolBytes from Media;

time
list volumes
time
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select storage=File1
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File1
stop_bacula

check_two_logs
check_restore_diff
end_test
